import java.sql.*;
import java.util.Scanner;

public class Jdbc {
    public static void main(String[] args) throws ClassNotFoundException, SQLException {

//        System.out.println("请输入姓名：");
//        Scanner scanner = new Scanner(System.in);
//        String userName = scanner.next();
//        System.out.println("请输入密码：");
//        String userPassword = scanner.next();
        logoin("孙六", "555");
    }


    public static void logoin(String userName, String userPassword) throws ClassNotFoundException, SQLException {
//        Connection connection = null;
//        Statement statement = null;
//        ResultSet resultSet = null;
        JdbcLj jdbcLj = null;
        try {
            jdbcLj = new JdbcLj();

            String sql = "select * from account where name = ? and password = ?";
            Object[] values = new Object[]{"孙六", "555"};
            ResultSet resultSet = jdbcLj.executeQuery(sql, values);
            ResultSetMetaData metaData = resultSet.getMetaData();
            int columnCount = metaData.getColumnCount();
            if (resultSet.next()) {
                System.out.println("登录成功！");
            } else {
                System.out.println("登录失败！");
            }


            //转账  从孙六转1到王五的money上
//            double m = 100;
            String tansfer = "update account set money = money - 1 where name = ?";
            Object[] into = new Object[]{"王五"};
            //执行事务
            int i = jdbcLj.executeUpdate(tansfer, into);
            if (i > 0) {
                System.out.println(userName + "转账成功！");
            } else {
                System.out.println(userName + "转账失败！");
            }

            //定义个异常
//            int s= 1/0;

            String intow = "update account set money = money + 1 where name = ?";
            Object[] arrw = new Object[]{"王五"};
            int i1 = jdbcLj.executeUpdate(intow, arrw);
            if (i1 > 0) {
                System.out.println("王五收到来自" + userName + "的转账！");
            } else {
                System.out.println("王五未收到来自" + userName + "的转账！");
            }

//            connection.commit();
        }
        /*
        catch (SQLException e) {
            try {
                connection.rollback();
            } catch (SQLException ex) {
                throw new RuntimeException(ex);
            }
            throw new RuntimeException(e);
        }*/ finally {
            jdbcLj.close();
        }
    }

}
